package com.yndsoft.jc.ui.memberpage.login;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;

import com.yndsoft.jc.model.KeyValue;
import com.yndsoft.jc.model.Member;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.utils.CountDownTimerUtil;
import com.yndsoft.jc.supplier.utils.IsInputWatcher;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.supplier.widget.dialog.SupplierDialog;
import com.yndsoft.jc.ui.memberpage.setting.AboutUsActivity;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;
import com.yndsoft.jc.utils.ConstantValues;

import java.lang.reflect.Type;

import butterknife.Bind;

/**
 * Created by hasee on 2017/12/6.
 */

public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.base_title)
    TextView base_title;

    @Bind(R.id.register_phone)
    EditText register_phone;
    @Bind(R.id.identifying_code)
    EditText identifying_code;
    @Bind(R.id.register_del)
    RelativeLayout register_del;
    @Bind(R.id.register_send_sms)
    Button register_send_sms;
    @Bind(R.id.register_pwd)
    EditText register_pwd;
    @Bind(R.id.register_next)
    Button register_next;
    private CountDownTimerUtil countDownTimer;

    @Bind(R.id.service_text)
    TextView service_text;
    @Bind(R.id.go_to_login)
    TextView go_to_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        setViews();
        setListner();
    }

    private void setViews() {
        base_title.setText(getResources().getString(R.string.register));

        service_text.setText(getClickableSpan());
        service_text.setHighlightColor(Color.TRANSPARENT);//去掉点击效果
        service_text.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private void setListner() {
        base_backBtn.setOnClickListener(this);
        register_send_sms.setOnClickListener(this);
        register_del.setOnClickListener(this);
        register_phone.addTextChangedListener(isInput);
        register_next.setOnClickListener(this);
        go_to_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
            case R.id.register_send_sms:
                String phoneSend = register_phone.getText().toString().trim();
                if (!TextUtils.isEmpty(phoneSend) && phoneSend.length() == 11) {
                    // TODO 根据手机号请求并接收验证码
                    countDownTimer = new CountDownTimerUtil(register_send_sms);
                    countDownTimer.start();
                    getMessage();
                } else {
                    ToastUtil.show(mContext, "请输入正确的手机号！");
                }
                break;
            case R.id.register_del:
                register_phone.setText("");
                break;
            case R.id.register_next:
                String phoneValue = register_phone.getText().toString().trim();
                String codeValue = identifying_code.getText().toString().trim();
                String pwdValue = register_pwd.getText().toString();
                if (TextUtils.isEmpty(phoneValue)) {
                    showHint("手机号不能为空！");
                } else {
                    if (phoneValue.length() != 11) {
                        showHint("请输入正确的手机号！");
                    } else {
                        if (TextUtils.isEmpty(codeValue)) {
                            showHint("验证码不能为空！");
                        } else {
                            if (codeValue.length() != 6) {
                                showHint("请输入正确的验证码！");
                            } else {
                                if (TextUtils.isEmpty(pwdValue)) {
                                    showHint("密码不能为空！");
                                } else {
                                    if (pwdValue.length() < 6 || pwdValue.length() > 20) {
                                        showHint("请输入6-20位密码！");
                                    } else {
                                        if (identifyingCode.equals(identifying_code.getText().toString().trim())) {
                                            registAccount();
                                        } else {
                                            showHint("验证码错误");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
            case R.id.go_to_login:
                finish();
                break;
        }
    }

    private void registAccount() {
        Member member = new Member();
        member.setPhone(register_phone.getText().toString());
        member.setPwd(register_pwd.getText().toString());
        mHttpUtil.registAcc(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        ToastUtil.show(mContext, "注册失败");
                        break;
                    case 1:
                        ToastUtil.show(mContext, "注册成功");
                        Type type = new TypeToken<Member>() {
                        }.getType();
                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                        Member member = (Member) fromJson;
                        mSession.setLogin(true);
                        mSession.setUserInfo(member);
//                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
//                        startActivity(intent);
                        setResult(RESULT_OK);
                        finish();
                        break;
                    case 2:
                        ToastUtil.show(mContext, "该手机号码已存在");
                        break;
                }
            }
        }, "reg", new Gson().toJson(member));
    }

    String identifyingCode;

    private void getMessage() {
        mHttpUtil.getSms(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        ToastUtil.show(mContext, "该号码已存在");
                        break;
                    case 1:
                        ToastUtil.show(mContext, "获取成功");
//                        Type type = new TypeToken<String>() {
//                        }.getType();
//                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
////                        KeyValue keyValue = (KeyValue) fromJson;
//                        identifyingCode = (String) fromJson;
                        identifyingCode = returnMsg.getInfo();
                        break;
                }
            }
        }, "shortmsg", register_phone.getText().toString().trim(), 1);
    }

    // 设置超链接文字
    private SpannableString getClickableSpan() {
        SpannableString spanStr = new SpannableString("注册即表示您同意《独角兽检测服务协议》");
        //demo1
        // 设置下划线文字
//        spanStr.setSpan(new UnderlineSpan(), 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 设置文字的单击事件
        spanStr.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                //点击加载H5
                Intent intent = new Intent(RegisterActivity.this, AboutUsActivity.class);
                intent.putExtra(ConstantValues.ABOUT_AD, 10029);
                startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);//去掉下划线
            }
        }, 11, 19, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 设置文字的前景色
        spanStr.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.mainColor)), 8, 19, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanStr;

    }

    private void showHint(String content) {
        new SupplierDialog(mContext).show(false, "提示", content);
    }

    private IsInputWatcher isInput = new IsInputWatcher() {
        @Override
        public void noInput() {
            register_del.setVisibility(View.INVISIBLE);
        }

        @Override
        public void inputting() {
            register_del.setVisibility(View.VISIBLE);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }
}

